<template>
  <div>
    <el-upload
      list-type="picture-card"
      :limit="1"
      action="#"
      :on-preview="handlePictureCardPreview"
      :on-remove="handleRemove"
      :on-change="addFile"
      :before-upload="beforeUpload"
      :http-request="upload"
      :file-list="fileList"
      :class="{onePicture:fileComputed}"
    >
      <i slot="default" class="el-icon-plus" />
    </el-upload>
    <el-dialog :visible.sync="dialogVisible" title="图片预览">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
    <el-progress v-show="showPerent" :percentage="percent" style="width:180px" />
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'
const cos = new COS({
  SecretId: 'AKIDud6IDqCI7fwwg9LGJ9k5B1buynMfkZ8R',
  SecretKey: '4tSlocASUHp79vSjkV0FExRfcJo33nHW'
})
export default {
  data() {
    return {
      dialogImageUrl: '',
      dialogVisible: false,
      fileList: [],
      currentFileUid: '',
      percent: 0,
      showPerent: false
    }
  },
  computed: {
    fileComputed() {
      return this.fileList.length === 1
    }
  },
  methods: {
    handleRemove(file, fileList) {
      this.fileList = fileList
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    addFile(file, fileList) {
      this.fileList = fileList.map(item => item)
    },
    beforeUpload(file) {
      // console.log(file)
      const types = ['image/jpeg', 'image/gif', 'image/bmp', 'image/png']
      if (!types.some(item => item === file.type)) {
        this.$message.error('图片格式只支持jpg,gif,bmp,png')
        return false
      }
      const maxSize = 5 * 1024 * 1024
      // console.log(maxSize)
      if (maxSize < file.size) {
        this.$message.error('图片大小不能大于5M')
        return false
      }
      // console.log(file)
      this.currentFileUid = file.uid
      this.showPerent = true
      return true
    },
    upload(params) {
      // console.log(params)
      if (params.file) {
        cos.putObject({
          Bucket: 'human-resources-1306439459', /* 必须 */
          Region: 'ap-chongqing', /* 存储桶所在地域，必须字段 */
          Key: params.file.name, /* 必须 */ // 文件名
          StorageClass: 'STANDARD',
          Body: params.file, // 上传文件对象
          onProgress: progressData => {
            // console.log(progressData.percent)
            this.percent = progressData.percent * 100
          }
        }, (err, data) => {
          // console.log(err || data)
          if (!err && data.statusCode === 200) {
            console.log(this.fileList)
            this.fileList = this.fileList.map(item => {
              if (item.uid === this.currentFileUid) {
                return { url: 'http://' + data.Location, upload: true }
              }
              return item
            })
          }
          setTimeout(() => {
            this.showPerent = false
            this.percent = 0
          }, 1000)
        })
      }
    }
  }
}
</script>

<style>
.onePicture .el-upload--picture-card{
  display: none;
}
</style>
